﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheOutLetLibrary.Dominio;
using System.Data.OleDb;

namespace TheOutLetLibrary.Infraestrutura {
    public class Accessk_skuDAO : BaseDAO, IGenericDAO<k_sku> {

        public Accessk_skuDAO(OleDbTransaction transacao)
            : base(transacao) {
        }

        public int Inclua(k_sku entity) {

            OleDbCommand comando = GetCommand();

            comando.CommandText =
                " INSERT INTO k_sku (" +
                " Id, ProductId, Variation, Weight, Width, Height, Depth) " +
                " VALUES (" +
                " @Id, @ProductId, @Variation, @Weight, @Width, @Height, @Depth) ";
            comando.Parameters.Add("@Id", OleDbType.Integer).Value = entity.Id;
            comando.Parameters.Add("@ProductId", OleDbType.Integer).Value = entity.ProductId;
            comando.Parameters.Add("@Variation", OleDbType.VarChar).Value = entity.Variation;
            comando.Parameters.Add("@Weight", OleDbType.Decimal).Value = entity.Weight;
            comando.Parameters.Add("@Width", OleDbType.Decimal).Value = entity.Width;
            comando.Parameters.Add("@Height", OleDbType.Decimal).Value = entity.Height;
            comando.Parameters.Add("@Depth", OleDbType.Decimal).Value = entity.Depth;
            comando.ExecuteNonQuery();

            comando.CommandText = "SELECT @@IDENTITY";
            entity.COD = Convert.ToInt32(comando.ExecuteScalar());
            return entity.COD;
        }

        public k_sku Ache(int Id) {

            OleDbCommand comando = GetCommand();

            comando.CommandText = "SELECT * FROM k_sku WHERE Id = @Id";
            comando.Parameters.Add("@Id", OleDbType.Integer).Value = Id;
            OleDbDataReader reader = comando.ExecuteReader();
            if(reader.HasRows) {
                return new k_sku {
                    Depth = reader.GetDecimal(reader.GetOrdinal("Depth")),
                    Height = reader.GetDecimal(reader.GetOrdinal("Height")),
                    Weight = reader.GetDecimal(reader.GetOrdinal("Weight")),
                    Width = reader.GetDecimal(reader.GetOrdinal("Width")),
                    Variation = reader.GetString(reader.GetOrdinal("Variation")),
                    ProductId = reader.GetInt32(reader.GetOrdinal("ProductId")),
                    Id = Id,
                    COD = reader.GetInt32(reader.GetOrdinal("COD"))
                };
            } else {
                return null;
            }
        }
    }
}
